package com.fhz.day007.coroutine

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.fhz.day007.R
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class Main2Activity : AppCompatActivity() {


     private lateinit var launch:Job
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        var api = Retrofit.Builder()
            .baseUrl("http://www.qubaobei.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build().create(Api::class.java)


        launch = GlobalScope.launch {
            val wxarticle1 = api.getWxarticle1()
            wxarticle1.data.forEach {
                println(it)
            }
        }


//        api.getWxarticle().enqueue(object : retrofit2.Callback<Food> {
//            override fun onFailure(call: Call<Food>, t: Throwable) {
//                Log.d("123", "onFailure: ${t.message}")
//            }
//
//            override fun onResponse(call: Call<Food>, response: retrofit2.Response<Food>) {
//                if (response.isSuccessful) {
//                    val wxArticleResponse = response.body().toString()
//                    println(wxArticleResponse)
//
//                }
//            }
//        })
    }

    override fun onDestroy() {
        super.onDestroy()
        launch.cancel()
    }
}
